import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class Auction extends TimerTask{
	
	Item item;
	ArrayList<User> bidderList;
	Server serverRefference;
	User seller, winner;
	Timer timer;
	private static final int AUCTION_TIMEOUT = 5;
	 	
    public Auction(Item itemForBid, User seller, Server server) {
        this.item = itemForBid;
        this.serverRefference = server;
        this.seller = new User();
        this.winner = new User();
        this.bidderList = new ArrayList<User>();
        this.seller.name = seller.name;
        this.seller.ipAddress = seller.ipAddress;
        this.seller.port = seller.port;
        timer = new Timer();
    }
    
    public Auction(String itemId, String description, Float startBid ,Float currentBid, String sellerName, String sellerIp, int sellerPort) {

        this.seller = new User();
        this.winner = new User();
        this.item = new Item(itemId, description, startBid, currentBid);
        this.seller.name = sellerName;
        this.seller.ipAddress = sellerIp;
        this.seller.port = sellerPort;
        this.bidderList = new ArrayList<User>();
        timer = new Timer();
    }

    private void auctionComplete() {
    	
    	System.out.println("the auction is over");
        //win
    	//sent to the winner
    	Message winMsg = new Message();
    	winMsg.type = "WIN"; 
    	winMsg.itemId = item.itemId;
    	winMsg.bid = item.currentBid.toString();
    	winMsg.port = winner.port;
    	winMsg.ipAddress = winner.ipAddress;
    	serverRefference.responseQ.put(winMsg);
            
        //sold to
    	//send to the seller
        Message soldTo = new Message();
        soldTo.type = "SOLDTO"; 
        soldTo.itemId = item.itemId;
        soldTo.bid = item.currentBid.toString();
        soldTo.port = seller.port;
        soldTo.ipAddress = seller.ipAddress;
		serverRefference.responseQ.put(soldTo);
		
		//bid-over
		//send to everyone except winner and seller
		for(User u : bidderList){
		
			if(u.name != seller.name || u.name == winner.name){
				//
			}else{
				
			 	Message bidOver = new Message();
		        bidOver.type = "BIDOVER"; 
		        bidOver.itemId = item.itemId;
		        bidOver.bid = item.currentBid.toString();
		        bidOver.port = u.port;
		        bidOver.ipAddress = u.ipAddress;
				serverRefference.responseQ.put(bidOver);

			}
		}
		
		try {
			serverRefference.userData.removeItem(item.itemId);
			serverRefference.userData.addTransaction(item.itemId, item.description, item.currentBid, winner.name, winner.ipAddress, winner.port);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		timer.cancel();

    }
    
    private void auctionRetry(){
        	
    	//send out new item message to all clients /w itemId
	    for(User u : serverRefference.userData.userList){
	    	
	    	System.out.println("restarting bid ....");
	    	Message newItemMsg2 = new Message();
	    	newItemMsg2.type = "NEW-ITEM";
	    	newItemMsg2.bid = item.currentBid.toString();
	    	newItemMsg2.itemId = item.itemId;
	    	newItemMsg2.description = item.description;
	    	newItemMsg2.ipAddress =  u.ipAddress;
	    	newItemMsg2.port = u.port;
	    	
	    	serverRefference.responseQ.put(newItemMsg2);

	    }    	
    	//timer will automatically fire after 5 minutes.
    }
         
	@Override
	public void run() {
		
		// TODO Auto-generated method stub	
		if(item.currentBid > item.startBid){
    		
    		auctionComplete();
    		
    	}else{
    		
    		auctionRetry();
    	}
	}

	public void start() {
		// TODO Auto-generated method stub
		timer.schedule(this, AUCTION_TIMEOUT*60*1000,AUCTION_TIMEOUT*60*1000);
	}

	public boolean hasBid(User winner2) {
		// TODO Auto-generated method stub
		for(User w : bidderList){
			if(w.name.equals(winner.name)){
				return true;
			}
		}
		return false;
	}
}
